/* eslint-disable no-unused-vars */
/* eslint-disable no-use-before-define */
import React, { useState, useRef } from "react";
import { Toast, Modal } from "antd-mobile";
import { dropByCacheKey } from "react-router-cache-route";
import { InterfaceG } from "@config/Config";
import SendMSGCode from "@component/sendMSGCode/sendMSGCode";
import yhTools from "@tools/yh";
let commonArgs = {};
/**证件更新 */
const decoratorToLoan = WrapperComponent => {
	return props => {
		const [showMsgBox, setShowMsgBox] = useState(false);
		const [sendSuccessPhone, setSendSuccessPhone] = useState("");
		const [orderNo, setOrderNo] = useState("");
		const sendCodeRef = useRef(null);
		//点击去借款
		const toLoan = args => {
			dropByCacheKey("xhdLoan");
			commonArgs = args;
			xhdSignApplyLoad(args);
		};
		//弹窗
		const alert = ({ availableAmount, duringtion, reminder }, name) => {
			Modal.alert(
				"",
				"根据国家相关法律要求，需要上传有效期内的身份影像后才可借款",
				[
					{ text: "取消", onPress: () => {} },
					{
						text: "上传身份证照片",
						onPress: () => {
							//跳转上传身份证页面
							console.log("--------高阶组件--------");
							jumpPage("/xhdUpIdCard", {
								availableAmount,
								duringtion,
								fromUrl: "xhdHome",
								reminder,
								name
							});
						}
					}
				]
			);
		};
		//跳转路由
		const jumpPage = (pathname, state = {}) => {
			props.history.push({
				pathname,
				state
			});
		};
		const xhdSignApplyLoad = (args = commonArgs) => {
			let objs = {
				url: InterfaceG.xhdSignApplyLoad,
				params: {
					productNo: "802"
				}
			};
			Toast.loading("加载中...", 100);
			yhTools.request(objs).then(res => {
				Toast.hide();
				if (res.code == "0") {
					sendCodeRef.current.countdown();
					if (res.data.status == "0") {
						setShowMsgBox(true);
						setSendSuccessPhone(res.data.bankMobile);
						setOrderNo(res.data.orderNo);
					} else if (res.data.status == "2") {
						alert(args, res.data.account_name);
					} else if (res.data.status == "1") {
						jumpLoan();
					}
				} else {
					Toast.info(res.message, 2);
				}
			});
		};
		//提交回调
		const submitCode = code => {
			xhdSignSubmitByVerifyCode(code);
		};
		const jumpLoan = () => {
			jumpPage("/xhdLoan", {
				fromUrl: "xhdHome"
			});
		};
		// 短信验证接口
		const xhdSignSubmitByVerifyCode = code => {
			let objs = {
				url: InterfaceG.xhdSignSubmitByVerifyCode,
				params: {
					verifyCode: code,
					orderNo: orderNo
				},
				_alertShow: false
			};
			Toast.loading("加载中...", 100);
			yhTools.request(objs).then(res => {
				if (res.code === "0") {
					closeSendModal();
					jumpLoan();
				} else {
					Toast.info(res.message, 2);
				}
			});
		};
		//关闭modal
		const closeSendModal = () => {
			setShowMsgBox(false);
		};

		return (
			<>
				<WrapperComponent {...props} toLoan={toLoan} />
				<SendMSGCode
					ref={sendCodeRef}
					getYZMCode={xhdSignApplyLoad}
					sendSuccessPhone={sendSuccessPhone}
					submitCode={submitCode}
					closeSendModal={closeSendModal}
					showMsgBox={showMsgBox}
				/>
			</>
		);
	};
};

export default decoratorToLoan;
